const state = {
    //初始化值
    formattedAddress:localStorage.formattedAddress || "",
    tips:[]

}
const mutations = {
     // 获得定位
   CHANGE_ADDRESS(state,address){
    //    console.log(address)
       state.formattedAddress = localStorage.formattedAddress = address
       console.log(address)
   },
   //地址栏搜索显示地理位置
   CHANGE_TIPS(state,tips){
    //  console.log(state,data)
       state.tips = tips
   }
}
const actions = {
    //进行我的异步操作 获得定位
    getFormattedAddress({ commit }){
       //假如localStorage中没有formattedAddress,则进行定位
       if(!localStorage.formattedAddress){
          commit('CHANGE_ADDRESS','高德定位正在启动...')
        AMap.plugin("AMap.Geolocation", function () {
        var geolocation = new AMap.Geolocation({
          // 是否使用高精度定位，默认：true
          enableHighAccuracy: true,
          // 设置定位超时时间，默认：无穷大
          timeout: 10000,
        });

      geolocation.getCurrentPosition();
      AMap.event.addListener(geolocation, "complete", onComplete);
      AMap.event.addListener(geolocation, "error", onError);

      function onComplete(data) {
        // data是具体的定位信息  
           
        commit('CHANGE_ADDRESS',data.formattedAddress)

      }

      function onError(data) {
        // 定位出错
      }
    });
    }
   },
   //地址栏搜索显示地理位置
   autoComplete({ commit },keyword){
          AMap.plugin('AMap.Autocomplete', function(){
        // 实例化Autocomplete
        var autoOptions = {
          //city 限定城市，默认全国
          city: '全国'
        }
        var autoComplete= new AMap.Autocomplete(autoOptions);
        autoComplete.search(keyword, function(status, result) {
          // 搜索成功时，result即是对应的匹配数据
          // console.log(result.tips)
          commit('CHANGE_TIPS',result.tips)
        })
      })

   }
}
export default {
    state,
    mutations,
    actions
}
